package com.coldchain.message.im.controller;

import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.common.redis.RedisUtils;
import com.coldchain.common.util.BeanUtils;
import com.coldchain.message.im.base.BaseIMAccount;
import com.coldchain.message.im.service.impl.IMAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/msg/im")
public class IMAccountController {
    private static final Logger log= LoggerFactory.getLogger(IMAccountController.class);

    @Autowired
    private IMAccountService imAccountService;

    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("/list")
    public Object list(){
        try {
            return  new ObjectRestResponse<>().data(imAccountService.getAll());
        }catch (Exception e){
            log.error("获取IM服务商失败,error:{}",e.getMessage());

            return new ObjectRestResponse<>().error("error");
        }

    }

    @PostMapping("/update")
    public Object update(@RequestParam Map<String,Object> account){
        Object ob=imAccountService.getAccountByType(account.get("imType").toString());
        if(ob!=null){
            try {
                 BeanUtils.map2BeanJava(account,ob);
                 imAccountService.editAccount((BaseIMAccount) ob);
                 return new ObjectRestResponse<>().data("ok");
            } catch (Exception e) {
                return  new ObjectRestResponse<>().error("参数错误");
            }
        }else {
            return new ObjectRestResponse<>().error("找不到对应的通道");
        }

    }



}
